﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBaoHanh.Template;
using BussinessLayer;
using QLBaoHanh.DoiBHSuaChua;
using DataAccessLayer;

namespace QLBaoHanh.QLTbiDangONCC
{
    public partial class frmDsTbiDangONCC : Form
    {
        private IEnumerable<View_ThietBiBHDetail> _dsThietBiBHSC;
        public IEnumerable<View_ThietBiBHDetail> DsThietBiBHSC
        {
            get { return _dsThietBiBHSC; }
            set 
            { 
                _dsThietBiBHSC = value;
                //reset binding
                LoadDataToGrid();
            }
        }


        public frmDsTbiDangONCC()
        {
            InitializeComponent();
            FillCboNhaCungCap();
            cboLocNhaCungCap.SelectedIndex = 0;
        }

        #region Methods

        private void FillCboNhaCungCap()
        {
            List<NhaCungCap> NhaCungCapList = new List<NhaCungCap>();
            NhaCungCapList = NhaCungCapBO.getInstance.GetAllNhaCungCap().ToList();
            NhaCungCap BlankNCC = new NhaCungCap();
            BlankNCC.Id = 0;
            BlankNCC.Ten = "- - - - - - - - Tất cả - - - - - - - - -";
            NhaCungCapList.Insert(0, BlankNCC);
            cboLocNhaCungCap.DataSource = NhaCungCapList;
            cboLocNhaCungCap.DisplayMember = "MaTenNhaCungCap";
            cboLocNhaCungCap.ValueMember = "Id";
        }

        /// <summary>
        /// Refesh form (grid)
        /// </summary>
        public void RefreshForm()
        {
            GetDataWithCurrentFilter();
        }

        /// <summary>
        /// Get selected item of grid view
        /// </summary>
        /// <returns>0 - not selected else return id</returns>
        private int getSelectedId()
        {
            int Id = 0 ;
            if (dataGridView1.SelectedRows.Count != 0)
            {
                Id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
            }
            return Id;
        }

        /// <summary>
        /// Load data to grid
        /// </summary>
        private void LoadDataToGrid()
        {
            int CurrentRowIndex = 0;
            if (dataGridView1.CurrentRow != null)
            {
                CurrentRowIndex = dataGridView1.CurrentRow.Index;
            }
            //getData with current filter
            //GetDataWithCurrentFilter();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = _dsThietBiBHSC;
            //if current row index !=0 then selected it
            if (CurrentRowIndex != 0 && CurrentRowIndex < dataGridView1.Rows.Count )
            {
                this.dataGridView1.Rows[CurrentRowIndex].Selected = true;
            }
            //else Selected to first items.
            if (dataGridView1.Rows.Count > 0 && dataGridView1.SelectedRows.Count == 0)
            {
                this.dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
            }
            int Phien_ThietBiBHIdSelected = getSelectedId();
            //LoadGridNhatKiBaoHanh(Phien_ThietBiBHIdSelected);
            //LoadGridCacLanGoiDien(Phien_ThietBiBHIdSelected);
            //LoadGridGhiChu(Phien_ThietBiBHIdSelected);
        }
      
        public void GetDataWithCurrentFilter()
        {
            int NCCId = 0;
            try
            {
                NCCId = Convert.ToInt32(cboLocNhaCungCap.SelectedValue);
            }
            catch (System.Exception ex)
            {
            	
            }
            DsThietBiBHSC = NghiepVuBO.getInstance.GetDsTbiDangONCCById(NCCId);
        }


        public void LoadGridCacLanGoiDien(int inPhien_ThietBiBHId)
        {
            dgvCacLanGoiDien.AutoGenerateColumns = false;
            if (inPhien_ThietBiBHId != 0)
            {
                dgvCacLanGoiDien.DataSource = LichSuBHBO.getInstance.GetLichSuBHByPhien_ThietBiId(inPhien_ThietBiBHId, MyCommon.LoaiLichSu.LichSuGoiDien);
            }
        }

        public void LoadGridGhiChu(int inPhien_ThietBiBHId)
        {
            dgvLuuY.AutoGenerateColumns = false;
            if (inPhien_ThietBiBHId != 0)
            {
                dgvLuuY.DataSource = LichSuBHBO.getInstance.GetLichSuBHByPhien_ThietBiId(inPhien_ThietBiBHId, MyCommon.LoaiLichSu.LichSuComment);
            }
        }
#endregion

        #region Events

        private void frmDsTbiDangONCC_Load(object sender, EventArgs e)
        {
            GetDataWithCurrentFilter();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int rowSelected = e.RowIndex;
                if (e.RowIndex != -1)
                {
                    this.dataGridView1.Rows[rowSelected].Selected = true;
                }
            }
        }
        
        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
        {
            // nothing is selected
            if (dataGridView1.SelectedRows.Count == 0)
                return;

            if (e.KeyCode == Keys.Enter)
            {
                e.Handled = true;
            }
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((e.Value is KhachHang) && (e.ColumnIndex == 6)) //TenKhachHang
            {
                e.Value = (e.Value as KhachHang).Ten;
            }
            else if ((e.Value is KhachHang) && (e.ColumnIndex == 7)) //SDT
            {
                e.Value = (e.Value as KhachHang).Mobile;
            }
            else if (e.Value is NhanVien)
            {
                e.Value = (e.Value as NhanVien).Ten;
            }
            else if (e.Value is TrangThaiPhien)
            {
                if ((e.Value as TrangThaiPhien).Id == (int)MyCommon.TrangThaiPhien.ThaoTacNgay_HoanThanh)
                {
                    e.Value = true;
                }
                else
                {
                    e.Value = false;
                }
                
            }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            int PhienId = getSelectedId();
            LoadGridCacLanGoiDien(PhienId);
            LoadGridGhiChu(PhienId);
        }

        private void btnGetData_Click(object sender, EventArgs e)
        {
            GetDataWithCurrentFilter();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnHoanThanh_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count != 0)
            {
                int Phien_ThietBiBHId = 0;
                string MaThietBi = "";
                Phien_ThietBiBHId = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                MaThietBi = dataGridView1.SelectedRows[0].Cells["MaThietBiBH"].Value.ToString();
                frmNhanVeTuNCC objFrm = new frmNhanVeTuNCC(Phien_ThietBiBHId, MaThietBi, this);
                objFrm.ShowDialog();
            }
        }


        private void cboLocNhaCungCap_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetDataWithCurrentFilter();
        }

        ///////////////////////////////////////////////////////////////////////////
        //Luu y

        private void dgvLuuY_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((e.Value is NhanVien) && (e.ColumnIndex == 2)) //Ma nhan vien
            {
                e.Value = (e.Value as NhanVien).Ma;
            }
            else if ((e.Value is NhanVien) && (e.ColumnIndex == 3)) //TenNV
            {
                e.Value = (e.Value as NhanVien).Ten;
            }
        }

        private void btnAddLuuY_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            Phien_ThietBiBHId = getSelectedId();
            if (Phien_ThietBiBHId == 0)
            {
                MessageBox.Show("Phải chọn thiết bị!", "Thêm lưu ý", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmThemLuuY frmObj = new frmThemLuuY(false, Phien_ThietBiBHId, this, 0);
            frmObj.ShowDialog();
        }

        private void btnEditLuuY_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            int LichSuId = 0;
            if (dgvLuuY.SelectedRows.Count != 0)
            {
                LichSuId = (int)dgvLuuY.SelectedRows[0].Cells["LuuYId"].Value;
            }
            else
            {
                return;
            }
            //Neu la lich su nguoi khac thi khong duoc sua
            if (!LichSuBHBO.getInstance.IsCoQuyen(LichSuId, frmMain.CurrentNhanVien.Id).RetValue)
            {
                MessageBox.Show("Không thể sửa lưu ý của nhân viên khác", "Sửa lưu ý", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Phien_ThietBiBHId = getSelectedId();
            frmThemLuuY frmObj = new frmThemLuuY(true, Phien_ThietBiBHId, this, LichSuId);
            frmObj.ShowDialog();
        }

        private void btnXoaLuuY_Click(object sender, EventArgs e)
        {
            int LichSuId = (int)dgvLuuY.SelectedRows[0].Cells["LuuYId"].Value;
            //Neu la lich su nguoi khac thi khong duoc sua
            if (!LichSuBHBO.getInstance.IsCoQuyen(LichSuId, frmMain.CurrentNhanVien.Id).RetValue)
            {
                MessageBox.Show("Không thể xóa lưu ý của nhân viên khác", "Xóa lưu ý", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (dgvLuuY.SelectedRows.Count != 0)
            {
                //TODO: confirm
                if (MessageBox.Show("Bạn chắc chắn muốn xóa nhật kí bảo hành, sửa chữa?"
                    , "Nhật kí bảo hành sửa chữa", MessageBoxButtons.YesNo
                    , MessageBoxIcon.Information) == DialogResult.Yes)
                {
                    //Tru chi phi sua chua cua thiet bi khi xoa ban ghi nay
                    LichSuBH aLichSu = LichSuBHBO.getInstance.GetLichSuBHById(LichSuId);
                    LichSuBHBO.getInstance.DelLichSuBH(LichSuId);
                    //Load lai grid nay
                    LoadGridGhiChu(getSelectedId());
                }
            }
        }

        private void btnLuuY_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            Phien_ThietBiBHId = getSelectedId();
            if (Phien_ThietBiBHId == 0)
            {
                MessageBox.Show("Phải chọn thiết bị!", "Thêm lưu ý", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmThemLuuY frmObj = new frmThemLuuY(false, Phien_ThietBiBHId, this, 0);
            frmObj.ShowDialog();
        }
        
        
        ////////////////////////////////////////////////////////////////////////////
        // Goi dien

        private void dgvCacLanGoiDien_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((e.Value is NhanVien) && (e.ColumnIndex == 2)) //Ma nhan vien
            {
                e.Value = (e.Value as NhanVien).Ma;
            }
            else if ((e.Value is NhanVien) && (e.ColumnIndex == 3)) //TenNV
            {
                e.Value = (e.Value as NhanVien).Ten;
            }
        }

        private void btnGoiDien_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            Phien_ThietBiBHId = getSelectedId();
            if(Phien_ThietBiBHId == 0)
            {
                MessageBox.Show("Phải chọn thiết bị!", "Thêm lần gọi điện", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmThemGoiDien frmObj = new frmThemGoiDien(false, Phien_ThietBiBHId, this, 0);
            frmObj.ShowDialog();
        }
        
        private void btnAddGoiDien_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            if (Phien_ThietBiBHId == 0)
            {
                MessageBox.Show("Phải chọn thiết bị!", "Thêm lần gọi điện", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            Phien_ThietBiBHId = getSelectedId();
            frmThemGoiDien frmObj = new frmThemGoiDien(false, Phien_ThietBiBHId, this, 0);
            frmObj.ShowDialog();
        }

        private void btnEditGoiDien_Click(object sender, EventArgs e)
        {
            int Phien_ThietBiBHId = 0;
            int LichSuId = 0;
            if (dgvCacLanGoiDien.SelectedRows.Count != 0)
            {
                LichSuId = (int)dgvCacLanGoiDien.SelectedRows[0].Cells["GoiDienId"].Value;
            }
            else
            {
                return;
            }
            //Neu la lich su nguoi khac thi khong duoc sua
            if (!LichSuBHBO.getInstance.IsCoQuyen(LichSuId, frmMain.CurrentNhanVien.Id).RetValue)
            {
                MessageBox.Show("Không thể sửa lần gọi điện của nhân viên khác", "Sửa lần gọi điện", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Phien_ThietBiBHId = getSelectedId();
            frmThemGoiDien frmObj = new frmThemGoiDien(true, Phien_ThietBiBHId, this, LichSuId);
            frmObj.ShowDialog();
        }

        private void btnDelGoiDien_Click(object sender, EventArgs e)
        {
            if (dgvCacLanGoiDien.SelectedRows.Count != 0)
            {
                int LichSuId = (int)dgvCacLanGoiDien.SelectedRows[0].Cells["GoiDienId"].Value;
                //Neu la lich su nguoi khac thi khong duoc sua
                if (!LichSuBHBO.getInstance.IsCoQuyen(LichSuId, frmMain.CurrentNhanVien.Id).RetValue)
                {
                    MessageBox.Show("Không thể xóa lần gọi điện của nhân viên khác", "Xóa lần gọi điện", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (MessageBox.Show("Bạn chắc chắn muốn xóa lịch sử gọi điện?"
                    , "Lịch sử gọi điện", MessageBoxButtons.YesNo
                    , MessageBoxIcon.Information) == DialogResult.Yes)
                {
                
                    //Tru chi phi sua chua cua thiet bi khi xoa ban ghi nay
                    LichSuBH aLichSu = LichSuBHBO.getInstance.GetLichSuBHById(LichSuId);
                    LichSuBHBO.getInstance.DelLichSuBH(LichSuId);
                    //Load lai grid nay
                    LoadGridCacLanGoiDien(getSelectedId());
                }
            }
        }
        
        #endregion
    }
}
